package com.fortune.rainshineservice.util;


import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;

/**
 * 地址转换坐标（Address translation coordinates）
 * @author tangyn
 */
public class AddTransCoordinate {
    private static final Logger logger = LoggerFactory.getLogger(AddTransCoordinate.class);

    private AddTransCoordinate(){

    }

    public static String baiduTranslate(String address){
        //百度地图接口
        StringBuilder apiBuilder = new StringBuilder();
        apiBuilder.append("https://api.map.baidu.com/geocoding/v3/");
        apiBuilder.append("?address="+address);
        String key = "RLnF5KWDWoXy6C0GxrXAMc7uRhfpAwep";
        apiBuilder.append("&output=json&ak="+key);
//        apiBuilder.append("&callback=showLocation");
        try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
            HttpGet httpGet = new HttpGet(apiBuilder.toString());
            HttpResponse httpResponse = httpClient.execute(httpGet);
            String e = EntityUtils.toString(httpResponse.getEntity());
            JsonParser jsonParser = new JsonParser();
            JsonElement jsonElement = jsonParser.parse(e);
            if(jsonElement.getAsJsonObject().get("status").getAsInt() == 0){
                Integer p = jsonElement.getAsJsonObject().get("result").getAsJsonObject().get("precise").getAsInt();
                if(p == 1){
                    JsonObject crObject = jsonElement.getAsJsonObject().get("result").getAsJsonObject().get("location").getAsJsonObject();
                    StringBuilder crBuilder = new StringBuilder();
                    crBuilder.append(crObject.get("lng"));
                    crBuilder.append(",");
                    crBuilder.append(crObject.get("lat"));
                    return crBuilder.toString();
                }else{
                    Integer c = jsonElement.getAsJsonObject().get("result").getAsJsonObject().get("comprehension").getAsInt();
                    if(c > 80){
                        JsonObject crObject = jsonElement.getAsJsonObject().get("result").getAsJsonObject().get("location").getAsJsonObject();
                        StringBuilder crBuilder = new StringBuilder();
                        crBuilder.append(crObject.get("lng"));
                        crBuilder.append(",");
                        crBuilder.append(crObject.get("lat"));
                        return crBuilder.toString();
                    }
                }
            }
        } catch (IOException e){
            logger.error("baiduTranslate ======> {}", e);
        }
        return null;
    }

    public static void main(String[] args){
        String ad = "青岛深圳路166号佰仕凯旋城4号楼1403";
        String position = baiduTranslate(ad);
        System.out.println(position);
    }

}
